package com.foton.websocket.controller;

import com.foton.websocket.pojo.rest.ApiResult;
import com.foton.websocket.utils.WeixinHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class RequestController {


        @RequestMapping(value = "/items")
    public UserMapper query(){
        UserMapper user = new UserMapper();
        user.setName("张三");
        user.setDescription("风度偏偏，潇洒少年！！！！");
        return user;
    }


    /**
     * 企业微信PC端跳转到默认浏览器页面
     * @return
     */
    @RequestMapping(value = "/wechat/openDefaultBrowser")
    public ModelAndView openDefaultBrowser(){
        ModelAndView model = new ModelAndView("index4");
        return model;
    }


    /**
     * 获取config接口注入权限验证配置
     * @param url
     * @return
     * @throws NoSuchAlgorithmException
     */


    @ResponseBody
    @RequestMapping(value = "/wechat/getWeiXinPermissionsValidationConfig")
    public ApiResult<Map<String, Object>> getWeiXinPermissionsValidationConfig(String url) throws NoSuchAlgorithmException {
        ApiResult<Map<String, Object>> apiResult = new ApiResult<Map<String, Object>>();
        Map<String, Object> resultMap = new HashMap<>(16);
        // 获取jsapi_ticket
        String ticket = WeixinHelper.getJsApiTicket(WeixinHelper.CRM_PC_CORPSECRET, "agent_config");
        //当前时间戳转成秒
        long timestamp = System.currentTimeMillis() / 1000;
        //随机字符串
        String nonceStr = "Wm3WZYTPz0wzccnW";
        // 获取JS-SDK使用权限签名
        String signature = WeixinHelper.getJSSDKSignature(ticket, nonceStr, timestamp, url);
        resultMap.put("corpid", WeixinHelper.APP_ID);
        resultMap.put("agentid", WeixinHelper.CRM_PC_AGENT_ID);
        resultMap.put("timestamp", timestamp);
        resultMap.put("nonceStr", nonceStr);
        resultMap.put("signature", signature);
        return apiResult.success(resultMap);
    }


    public static void main(String[] args) throws NoSuchAlgorithmException {
        //当前时间戳转成秒
        long timestamp = System.currentTimeMillis() / 1000;

        //随机字符串
        String nonceStr = "Wm3WZYTPz0wzccnW";

        String ticket = WeixinHelper.getJsApiTicket(WeixinHelper.CRM_PC_CORPSECRET, "");

        String signature= WeixinHelper.getJSSDKSignature(ticket, nonceStr, timestamp, "http://localhost:8066/qw/wechat/openDefaultBrowser");

        System.out.println(signature);


    }





}
